package com.smile.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smile.commons.page.R;
import com.smile.model.dto.request.AddMediaShareRequestDTO;
import com.smile.model.dto.request.GetMediaDeviceRequestDTO;
import com.smile.model.dto.request.GetMediaRequestDTO;
import com.smile.model.request.*;
import com.smile.model.response.response.GetMediaDeviceResponseDTO;
import com.smile.model.response.response.GetMediaInfoResponseDTO;
import com.smile.model.response.response.GetMediaResponseDTO;
import com.smile.model.response.response.GetMediaShareResponseDTO;
import com.smile.service.MediaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.print.attribute.standard.Media;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/media")
public class MediaController {

    @Autowired
    private MediaService mediaService;

//    @Autowired
//    private DeviceClient deviceClient;
//
//    @PostMapping("/test/topic")
//    public R test(){
//        return R.ok();
//    }

    @PostMapping(value = "/device")
    public R<List<GetMediaDeviceResponseDTO>> addDrone(@Valid @RequestBody GetMediaDeviceRequestDTO getMediaDeviceRequestDTO) throws IOException {
        List<GetMediaDeviceResponseDTO> getMediaDeviceResponseDTOList = mediaService.getDeviceList(getMediaDeviceRequestDTO);
        return R.ok(getMediaDeviceResponseDTOList);
    }

    @PostMapping(value = "/directory")
    public R<Page<GetMediaResponseDTO>> directory(@Valid @RequestBody GetMediaRequestDTO getMediaRequestDTO) throws IOException {
        Page<GetMediaResponseDTO> getMediaResponseDTOList = mediaService.getMediaDirectory(getMediaRequestDTO);
        return R.ok(getMediaResponseDTOList);
    }

    @PostMapping(value = "/directoryAll")
    public R<List<GetMediaResponseDTO>> directoryAll(@Valid @RequestBody GetMediaRequestDTO getMediaRequestDTO) throws IOException {
        List<GetMediaResponseDTO> getMediaResponseDTOList = mediaService.getMediaDirectoryAll(getMediaRequestDTO);
        return R.ok(getMediaResponseDTOList);
    }

    @PostMapping(value = "/updateMedia")
    public R updateMedia(@Valid @RequestBody GetMediaRequestDTO getMediaRequestDTO) throws IOException {
        java.lang.Boolean result = mediaService.updateMedia(getMediaRequestDTO);
        if (result) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    @PostMapping(value = "/deleteMedia")
    public R deleteMedia(@Valid @RequestBody GetMediaRequestDTO getMediaRequestDTO) throws IOException {
        java.lang.Boolean result = mediaService.deleteMedia(getMediaRequestDTO);
        if (result) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    @PostMapping(value = "/batchDeleteMedia")
    public R batchDeleteMedia(@Valid @RequestBody GetMediaRequestDTO getMediaRequestDTO) throws IOException {
        java.lang.Boolean result = mediaService.batchDeleteMedia(getMediaRequestDTO);
        if (result) {
            return R.ok();
        } else {
            return R.fail();
        }
    }


    @PostMapping(value = "/batchDownload")
    public R batchDeleteDown(@Valid @RequestBody GetMediaRequestDTO getMediaRequestDTO) throws IOException {
        String result = mediaService.batchDownload(getMediaRequestDTO);
        return R.ok(result);
    }

    /**
     * 目录下载
     * @param getMediaRequestDTO
     * @return
     * @throws IOException
     */
    @PostMapping(value = "/directoryDownload")
    public R directoryDownload(@Valid @RequestBody GetMediaRequestDTO getMediaRequestDTO) throws IOException {
        String result = mediaService.directoryDownload(getMediaRequestDTO);
        return R.ok(result);
    }



    @PostMapping(value = "/mediaInfo")
    public R<GetMediaInfoResponseDTO> mediaInfo(@Valid @RequestBody GetMediaRequestDTO getMediaRequestDTO) throws IOException {
        GetMediaInfoResponseDTO getMediaInfoResponseDTO = mediaService.getMediaInfo(getMediaRequestDTO.getId());
        if (ObjectUtil.isNotNull(getMediaInfoResponseDTO)) {
            return R.ok(getMediaInfoResponseDTO);
        } else {
            return R.fail();
        }
    }

    @PostMapping(value = "/addMediaShare")
    public R<GetMediaShareResponseDTO> addMediaShare(@Valid @RequestBody AddMediaShareRequestDTO addMediaShareRequestDTO) throws IOException {
        GetMediaShareResponseDTO getMediaInfoResponseDTO = mediaService.addMediaShare(addMediaShareRequestDTO);
//        GetMediaInfoResponseDTO getMediaInfoResponseDTO = mediaService.getMediaInfo(getMediaRequestDTO.getId());
        if (ObjectUtil.isNotNull(getMediaInfoResponseDTO)) {
            return R.ok(getMediaInfoResponseDTO);
        } else {
            return R.fail();
        }
    }


}
